package com.wtwd.campus.attendancereceiver;

import com.wtwd.campus.attendancenetty.AttendanceChannelMap;
import com.wtwd.campus.entity.AttendanceSendData;
import com.wtwd.campus.entity.AttendanceSendDataList;
import com.wtwd.campus.service.InjectFeignService;
import com.wtwd.campus.utils.InjectService;
import io.netty.channel.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

public class SendDataReceiver implements AttendanceReceiver {
    private  static final Logger logger= LoggerFactory.getLogger(SendDataReceiver.class);
    @Override
    public String parseMessage(String data, Channel channel) {
        //首先进行校验位的校验
        boolean b = judgeSecurity(data);
        if(b){
            //校验成功.截取信息
            String serialNo=data.substring(8,10);
           /* String productNo = AttendanceChannelMap.getKey(channel);*/
            String productNo = "A_32871326";
            //判断是否一分钟内重复提交
            boolean b1 = InjectService.judgeIfExist(serialNo.concat("_").concat(productNo));
            if(!b1){
                //如果不存在，则进行操作,并进行存储
                InjectService.saveAttendanceRepeat(serialNo.concat("_").concat(productNo));
                Integer num=Integer.valueOf(data.substring(10,12));
                ArrayList<AttendanceSendData> list=new ArrayList<>();
                for(int i=0;i<num;i++){
                    int start=12+(16*i);
                    String cardNo = data.substring(start, start + 16);
                    String ifLowBat=cardNo.substring(0,2);
                    String realCardNo = cardNo.substring(6);
                    int i1 = realCardNo.lastIndexOf("00");
                    realCardNo=realCardNo.substring(i1+2);
                    String status = data.substring(start+16, start + 18);
                    String timeStr = data.substring(start + 18);
                    Long time = convertTime(timeStr);
                    AttendanceSendData attendanceSendData=new AttendanceSendData();
                    attendanceSendData.setCardNo(realCardNo);
                    attendanceSendData.setIfLowBattery(ifLowBat.equals("01")?true:false);
                    attendanceSendData.setStatus(Integer.valueOf(status));
                    attendanceSendData.setTime(time);
                    list.add(attendanceSendData);
                }
                AttendanceSendDataList attendanceSendDataList=new AttendanceSendDataList();
                attendanceSendDataList.setSerialNo(serialNo);
                attendanceSendDataList.setList(list);
                attendanceSendDataList.setProductNo(productNo.split("_")[1]);
                // 远程调用
                InjectFeignService.getIrmFeignClientService().sendAttendanceData(attendanceSendDataList);
            }else{
                return productNo;
            }
        }
        return null;
    }

    /**
     * 将年月日转成毫秒格式
     * @param data
     * @return
     */
    public Long convertTime(String data){
        Integer year=Integer.parseInt(data.substring(0,4),16);
        Integer month=Integer.parseInt(data.substring(4,6),16);
        Integer day=Integer.parseInt(data.substring(6,8),16);
        Integer hour=Integer.parseInt(data.substring(8,10),16);
        Integer min=Integer.parseInt(data.substring(10,12),16);
        Integer second=Integer.parseInt(data.substring(12,14),16);
        String yearStr=String.valueOf(year);
        String monthStr=month<10?"0".concat(String.valueOf(month)):String.valueOf(month);
        String dayStr=day<10?"0".concat(String.valueOf(day)):String.valueOf(day);
        String hourStr=hour<10?"0".concat(String.valueOf(hour)):String.valueOf(hour);
        String minStr=min<10?"0".concat(String.valueOf(min)):String.valueOf(min);
        String secondStr=second<10?"0".concat(String.valueOf(second)):String.valueOf(second);
        String time=yearStr.concat(monthStr).concat(dayStr).concat(hourStr).concat(minStr).concat(secondStr);
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        Calendar calendar=Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(time));
        } catch (ParseException e) {
            e.printStackTrace();
            logger.info("时间格式有误："+time);
        }
        return calendar.getTimeInMillis();
    }


}
